<?php
// $username = "zhaoritian";
// $password = "123456";
// $data = json_decode($_POST(),true);
$data = file_get_contents('php://input');
$data = json_decode($data, true);
$username = $data['username'];
$password = $data["password"];

$servername = 'localhost';
$dbusername = 'username';
$dbpassword = 'password';
$dbname = 'testDB';

// 创建连接
$connect = mysqli_connect($servername, $dbusername, $dbpassword);
 
// 检测连接
if (!$connect) {
    die("Connection failed: " . mysqli_connect_error() . "<br />");
}
// 选择数据库
mysqli_select_db($connect, $dbname);
// 设置编码，防止中文乱码
mysqli_query($connect, "set names utf8");

// 查询用户数据并输出
$sql = 'SELECT id, username, password, token, email, phone, collection FROM users WHERE username="' . $username . '"AND password="' . $password . '"';
$retval = mysqli_query($connect, $sql);
$result['success'] = false;
$result['message'] = '';
if (!$retval) {
    die('ERROR: ' . mysqli_error($connect));
} else {
    // 使用 mysqli_fetch_assoc 获取数据
    $row = mysqli_fetch_assoc ($retval);
    $result['docs']["id"] = $row['id'];
    $result['docs']["username"] = $row['username'];
    $result['docs']["password"] = $row['password'];
    $result['docs']["token"] = $row['token'];
    $result['docs']["email"] = $row['email'];
    $result['docs']["phone"] = $row['phone'];

    // 查询收藏数据
    $sqlCL = 'SELECT name FROM ' . $row['collection'] . ' ORDER BY id';
    $retvalCL = mysqli_query($connect, $sqlCL);
    if (!$retvalCL) {
        die('ERROR: ' . mysqli_error($connect));
    } else {
        while ($rowCL = mysqli_fetch_assoc ($retvalCL)) {
            $result['docs']['collection'][] = "{$rowCL['name']}";
        }
    }
    $result['success'] = true;
    $result['message'] = urlencode('查询成功！');
    // 释放内存
    mysqli_free_result($retval);
    mysqli_free_result($retvalCL);
}

echo urldecode(json_encode($result));
